package com.situ.mall.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.manager.entity.Goods;
import com.situ.mall.manager.entity.GoodsPic;
import com.situ.mall.manager.mapper.GoodsMapper;
import com.situ.mall.manager.service.GoodsPicService;
import com.situ.mall.manager.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//实现service接口
//添加service注解 将实现类放进spring的bean容器
@Service
public class GoodsServiceImpl implements GoodsService {

    //注入mapper层对象 还可以用resouce  优先使用类型 Autowired优先使用名字
    @Resource
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsPicService goodsPicService;

    @Override
    /**
     * 使用spring的事务管理
     */
    @Transactional(
            //隔离级别设置为最高级别
            isolation = Isolation.SERIALIZABLE, //事务的四大特性 原子 一致 隔离 持久

            //spring事务的传播机制
            propagation = Propagation.REQUIRED ,

            //回滚
            rollbackFor = Exception.class   //当方法抛出任何异常就回滚，没有异常就提交事务


    )
    public int add(Goods goods) throws Exception {
        //验证参数
        if (goods.getName()==null || goods.getName().length()==0) {
            throw new Exception("分类名不能为空");
        }
        if (goods.getRecom()==null) {
            throw new Exception("推荐不能为空");
        }
        //逻辑处理

        //先保存商品再保存图片
        //1、商品保存 获取id主键
        int code = goodsMapper.insert(goods);
        if(code > 0 && !goods.getPics().isEmpty()){
            //2、保存图片
            for(GoodsPic pic :goods.getPics()){
                pic.setGoodsId(goods.getId());
            }
            goodsPicService.addPatch(goods.getPics());
        }

        //访问数据库
        return code;
    }

    @Override
    public int remove(Integer id) throws Exception {
        Goods goods = goodsMapper.selectById(id);
        if (goods == null) {
            throw new Exception("id不存在");
        }
        //只会删除goods不会删除图片 另补
        return goodsMapper.delete(id);
    }

    @Override
    public int edit(Goods goods) throws Exception {
       Goods sGoods = goodsMapper.selectById(goods.getId());
        if (sGoods == null) {
            throw new Exception("id不存在");
        }
        //只修改了goods没有修改图片 另补
        return goodsMapper.update(goods);
    }

    @Override
    public Goods getById(Integer id) throws Exception{
        Goods goods = goodsMapper.selectById(id);
        if(goods == null){
            throw new Exception("id不存在");
        }
        return goods;
    }

    @Override
    public List<Goods> search(Goods goods) {
        return goodsMapper.select(goods);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Goods goods) {
        //分三步
        //1.启动分页查询
        PageHelper.startPage(page,limit);

        //2.调用Mapper
        List<Goods> list =  goodsMapper.select(goods);

        //3.构造PageInfo
        PageInfo pageInfo = new PageInfo(list);

        return pageInfo;

    }


}
